//
//  gxdy_Contact.m
//  OCQ_API_Test
//
//  Created by 故乡的云  on 2017/5/5.
//  Copyright © 2017年 故乡的云 . All rights reserved.
//

#import "gxdy_Contact.h"
#import "NSDictionary+Obj2Dict.h"

@implementation gxdy_Contact
+ (instancetype _Nonnull )contactWithContact:(CNContact *_Nonnull)contact {
    gxdy_Contact *model = [self new];
    model.identifier = contact.identifier;
    model.contactType = contact.contactType;
    model.namePrefix = contact.namePrefix;
    model.givenName = contact.givenName;
    model.middleName = contact.middleName;
    model.familyName = contact.familyName;
    model.previousFamilyName = contact.previousFamilyName;
    model.nameSuffix = contact.nameSuffix;
    model.nickname = contact.nickname;
    model.organizationName = contact.organizationName;
    model.departmentName = contact.departmentName;
    model.jobTitle = contact.jobTitle;
    model.phoneticGivenName = contact.phoneticGivenName;
    model.phoneticMiddleName = contact.phoneticMiddleName;
    model.phoneticFamilyName = contact.phoneticFamilyName;
    model.phoneticOrganizationName = contact.phoneticOrganizationName;
    model.note = contact.note;
    model.imageData = contact.imageData;
    model.thumbnailImageData = contact.thumbnailImageData;
    model.imageDataAvailable = contact.imageDataAvailable;
    model.phoneNumbers = contact.phoneNumbers;
    model.emailAddresses = contact.emailAddresses;
    model.postalAddresses = contact.postalAddresses;
    model.urlAddresses = contact.urlAddresses;
    model.contactRelations = contact.contactRelations;
    model.socialProfiles = contact.socialProfiles;
    model.instantMessageAddresses = contact.instantMessageAddresses;
    model.birthday = contact.birthday;
    model.nonGregorianBirthday = contact.nonGregorianBirthday;
    model.dates = contact.dates;
    
    return model;
}
+ (instancetype _Nonnull )contactWithRecordRef:(ABRecordRef _Nonnull)recordRef{
    gxdy_Contact *model = [self new];
    // 获取当前联系人名字
    NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(recordRef, kABPersonFirstNameProperty));
    model.givenName = firstName;
    
    // 获取当前联系人姓氏
    NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(recordRef, kABPersonLastNameProperty));
    model.familyName = lastName;
//    NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
    
    // 获取当前联系人的电话 数组
    NSMutableArray *phoneArray = [[NSMutableArray alloc] init];
    ABMultiValueRef phones = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
    for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
        NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j));
//        NSLog(@"phone=%@", phone);
        [phoneArray addObject:phone];
    }
    model.phoneNumbers = phoneArray;
    
    // 获取当前联系人的邮箱 注意是数组
    NSMutableArray *emailArray = [[NSMutableArray alloc]init];
    ABMultiValueRef emails= ABRecordCopyValue(recordRef, kABPersonEmailProperty);
    for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
        NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j));
//        NSLog(@"email=%@", email);
        [emailArray addObject:email];
    }
    model.emailAddresses = emailArray;
    
    // 获取当前联系人中间名
    NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonMiddleNameProperty));
    model.middleName = middleName;
    
    // 获取当前联系人的名字前缀
    NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonPrefixProperty));
    model.namePrefix = prefix;
    
    // 获取当前联系人的名字后缀
    NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonSuffixProperty));
    model.nameSuffix = suffix;
    
    // 获取当前联系人的昵称
    NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonNicknameProperty));
    model.nickname = nickName;
    
    // 获取当前联系人的名字拼音
    NSString *firstNamePhonetic=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonFirstNamePhoneticProperty));
    model.phoneticGivenName = firstNamePhonetic;
    
    // 获取当前联系人的姓氏拼音
    NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonLastNamePhoneticProperty));
    model.phoneticFamilyName = lastNamePhoneic;
    
    // 获取当前联系人的中间名拼音
    NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonMiddleNamePhoneticProperty));
    model.phoneticMiddleName = middleNamePhoneic;
    
    // 获取当前联系人的公司
    NSString *organization=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonOrganizationProperty));
    model.organizationName = organization;
    
    // 获取当前联系人的职位
    NSString *job=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonJobTitleProperty));
    model.jobTitle = job;

    // 获取当前联系人的部门
    NSString *department=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonDepartmentProperty));
    model.departmentName = department;

    // 获取当前联系人的生日
    NSDateComponents *birthday=(__bridge NSDateComponents*)(ABRecordCopyValue(recordRef, kABPersonBirthdayProperty));
    model.birthday = birthday;
    
    // 获取当前联系人的备注
    NSString *notes=(__bridge NSString*)(ABRecordCopyValue(recordRef, kABPersonNoteProperty));
    model.note = notes;

    // 获取当前联系人头像图片
    NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(recordRef));
    model.imageData = userImage;
/** 待完善
    // 获取创建当前联系人的时间 注意是NSDate
    NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(recordRef, kABPersonCreationDateProperty));

    // 获取最近修改当前联系人的时间
    NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(recordRef, kABPersonModificationDateProperty));
    
    // 获取地址
    ABMultiValueRef address = ABRecordCopyValue(recordRef, kABPersonAddressProperty);
    for (int j=0; j<ABMultiValueGetCount(address); j++) {
        // 地址类型
        NSString *type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
        NSDictionary * tempDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
        // 地址字符串，可以按需求格式化
        NSString *adress = [NSString stringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",
                            [tempDic valueForKey: (NSString*)kABPersonAddressCountryKey],[tempDic valueForKey:(NSString*)kABPersonAddressStateKey],[tempDic valueForKey:(NSString*)kABPersonAddressCityKey],[tempDic valueForKey:(NSString*)kABPersonAddressStreetKey],[tempDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
    }
//    model.postalAddresses = address;
    
    // 获取当前联系人纪念日
    NSMutableArray *dateArr = [[NSMutableArray alloc] init];
    ABMultiValueRef dates= ABRecordCopyValue(recordRef, kABPersonDateProperty);
    for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
        // 获取纪念日日期
        NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
        // 获取纪念日名称
        NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
        NSDictionary *tempDic = [NSDictionary dictionaryWithObject:data forKey:str];
        [dateArr addObject:tempDic];
    }
*/
    
    return model;
}

#pragma mark - 归档 、 解档
+ (BOOL)supportsSecureCoding {
    return YES;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    // code...

    
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    // code...
    return self;
}
@end
